# Maintainer: Martell Malone <martellmalone@gmail.com>

_realname=nodejs

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.10.29
pkgrel=1
pkgdesc="Evented I/O for V8 javascript (mingw-w64)"
arch=('any')
url="http://nodejs.org/"
license=("MIT")
makedepends=("python2" "make" "gyp-svn")
depends=("${MINGW_PACKAGE_PREFIX}-c-ares" "${MINGW_PACKAGE_PREFIX}-http-parser" "${MINGW_PACKAGE_PREFIX}-v8" "${MINGW_PACKAGE_PREFIX}-libuv" "${MINGW_PACKAGE_PREFIX}-openssl" "${MINGW_PACKAGE_PREFIX}-zlib")
options=('!emptydirs !strip')
source=("http://nodejs.org/dist/v${pkgver}/node-v${pkgver}.tar.gz"
        "node_mingw64.patch")
md5sums=('SKIP'
         'SKIP')

prepare() {
  cd ${srcdir}/node-v${pkgver}
  patch -p0 -i ${srcdir}/node_mingw64.patch
  

  msg 'fixing for python2 name'
  find -type f -exec sed -e 's_^#!/usr/bin/env python$_&2_' -e 's_^\(#!/usr/bin/python2\).[45]$_\1_' -e 's_^#!/usr/bin/python$_&2_' -e "s_'python'_'python2'_" -i {} \;
  find test -type f -exec sed -e "s|python |python2 |" -i {} \;
  sed -i "s|python |python2 |" Makefile

}

build() {

  export PYTHON=/usr/bin/python2

  # todo: out of source build doesn't work atm.
  #export PATH="${srcdir}/node-v${pkgver}:$PATH"
  #mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  #cd "${srcdir}/build-${MINGW_CHOST}"
  # dirty hack to find 'tools/*.py' in out of source build with love from martell
  #cp -r ${srcdir}/node-v${pkgver}/tools ${srcdir}/build-${MINGW_CHOST}
  #cp -r ${srcdir}/node-v${pkgver}/node.gyp ${srcdir}/build-${MINGW_CHOST}

  cd ${srcdir}/node-v${pkgver}

  PATH="${srcdir}/node-v${pkgver}:$PATH" \
  CC=${MINGW_PREFIX}/bin/gcc \
  CXX=${MINGW_PREFIX}/bin/g++ \
  ${srcdir}/node-v${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --shared-v8 \
    --shared-http-parser \
    --shared-cares \
    --shared-openssl \
    --shared-libuv \
    --shared-zlib

  #python2 tools/gyp_node.py -f make
  #LINK=${MINGW_PREFIX}/bin/g++ make -C out BUILDTYPE=Release V=1
  make
}

package() {
  cd "${srcdir}/node-v${pkgver}"
  #cd "${srcdir}/build-${MINGW_CHOST}"
  make install DESTDIR="${pkgdir}"
}
